package com.invoicerecognition.backend.model.dto;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.invoicerecognition.backend.handler.ListMapTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Invoice {
    @TableId(type = IdType.AUTO)    // 实现自增
    private Integer id;
    private Integer userId;

    @TableField("PurchaserAddress")
    private String PurchaserAddress;

    @TableField("PurchaserBank")
    private String PurchaserBank;

    @TableField("Password")
    private String Password;

    @TableField(value = "CommodityVehicleType", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityVehicleType;  // none

    @TableField("SellerRegisterNum")
    private String SellerRegisterNum;

    @TableField("SellerBank")
    private String SellerBank;

    @TableField(value = "CommodityNum", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityNum;  // none

    @TableField(value = "CommodityAmount", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityAmount;

    @TableField("InvoiceType")
    private String InvoiceType;

    @TableField("AmountInWords")
    private String AmountInWords;

    @TableField("TotalTax")
    private String TotalTax;

    @TableField("MachineCode")
    private String MachineCode;

    @TableField("City")
    private String City;

    @TableField("InvoiceNumDigit")
    private String InvoiceNumDigit;

    @TableField("Checker")
    private String Checker;

    @TableField("InvoiceCode")
    private String InvoiceCode;

    @TableField("SellerAddress")
    private String SellerAddress;

    @TableField(value = "CommodityPrice", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityPrice;    // none

    @TableField("NoteDrawer")
    private String NoteDrawer;

    @TableField("Province")
    private String Province;

    @TableField("InvoiceNum")
    private String InvoiceNum;

    @TableField(value = "CommodityTaxRate", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityTaxRate;

    @TableField("ServiceType")
    private String ServiceType;

    @TableField("InvoiceDate")
    private String InvoiceDate;

    @TableField(value = "CommodityEndDate", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityEndDate;  // none

    @TableField("PurchaserRegisterNum")
    private String PurchaserRegisterNum;

    @TableField(value = "CommodityStartDate", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityStartDate;    // none

    @TableField("TotalAmount")
    private String TotalAmount;
    @TableField("SheetNum")
    private String SheetNum;

    @TableField(value = "CommodityPlateNum", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityPlateNum;     // none

    @TableField("PurchaserName")
    private String PurchaserName;

    @TableField("SellerName")
    private String SellerName;

    @TableField("InvoiceNumConfirm")
    private String InvoiceNumConfirm;

    @TableField("Agent")
    private String Agent;

    @TableField("InvoiceTag")
    private String InvoiceTag;

    @TableField(value = "CommodityUnit", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityUnit;     // none

    @TableField("CheckCode")
    private String CheckCode;

    @TableField("InvoiceTypeOrg")
    private String InvoiceTypeOrg;

    @TableField("InvoiceCodeConfirm")
    private String InvoiceCodeConfirm;

    @TableField("Remarks")
    private String Remarks;

    @TableField("Payee")
    private String Payee;

    @TableField(value = "CommodityTax", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityTax;

    @TableField("AmountInFiguers")
    private String AmountInFiguers;

    @TableField(value = "CommodityName", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityName;

    @TableField(value = "CommodityType", typeHandler = ListMapTypeHandler.class)
    private List<Map<String, String>> CommodityType;     // none

    @TableField("OnlinePay")
    private String OnlinePay;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private Date createtime;

    private String invoicePhoto;

    // Setter 方法示例
    public void setCommodityVehicleType(String commodityVehicleTypeJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityVehicleType = objectMapper.readValue(commodityVehicleTypeJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityVehicleType = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityTaxRate(String commodityTaxRateJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityTaxRate = objectMapper.readValue(commodityTaxRateJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityTaxRate = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityNum(String commodityNumJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityNum = objectMapper.readValue(commodityNumJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityNum = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityAmount(String commodityAmountJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityAmount = objectMapper.readValue(commodityAmountJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityAmount = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityPrice(String commodityPriceJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityPrice = objectMapper.readValue(commodityPriceJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityPrice = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityEndDate(String commodityEndDateJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityEndDate = objectMapper.readValue(commodityEndDateJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityEndDate = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityStartDate(String commodityStartDateJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityStartDate = objectMapper.readValue(commodityStartDateJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityStartDate = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityPlateNum(String commodityPlateNumJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityPlateNum = objectMapper.readValue(commodityPlateNumJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityPlateNum = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityUnit(String commodityUnitJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityUnit = objectMapper.readValue(commodityUnitJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityUnit = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityTax(String commodityTaxJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityTax = objectMapper.readValue(commodityTaxJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityTax = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityName(String commodityNameJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityName = objectMapper.readValue(commodityNameJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityName = new ArrayList<>(); // 处理异常情况
        }
    }

    public void setCommodityType(String commodityTypeJson) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.CommodityType = objectMapper.readValue(commodityTypeJson, new TypeReference<List<Map<String, String>>>() {});
        } catch (Exception e) {
            this.CommodityType = new ArrayList<>(); // 处理异常情况
        }
    }

}
